//
// Created by jie.jiang on 2022/12/2.
//

#include "time_utils.h"
#include "stdint.h"
#include "stm32f1xx_hal.h"

uint32_t m,tms,u,t0;

__STATIC_INLINE uint32_t LL_SYSTICK_IsActiveCounterFlag()
{
  return ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == (SysTick_CTRL_COUNTFLAG_Msk));
}
uint64_t micros()
{
  LL_SYSTICK_IsActiveCounterFlag();
  m = HAL_GetTick();
  tms = SysTick->LOAD+1;
  u = tms - SysTick->VAL;
  if (LL_SYSTICK_IsActiveCounterFlag())
  {
    m = HAL_GetTick();
    u = tms - SysTick->VAL;
  }
  return (m*1000+(u*1000)/tms);
}